﻿using System;
using System.Windows;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace SilverXNA
{
	public partial class GamePage2 : PhoneApplicationPage
	{
		ContentManager contentManager;
		GameTimer timer;
		SpriteBatch spriteBatch;

		// For rendering the XAML onto a texture
		UIElementRenderer elementRenderer;
		int elementRendererHeight;
		int elementRendererWidth;
        UIElementRenderer elementRenderer2;
		int elementRenderer2Height;
		int elementRenderer2Width;

		public GamePage2()
		{
			InitializeComponent();

			// Get the content manager from the application
			contentManager = (Application.Current as App).Content;

			// Create a timer for this page
			timer = new GameTimer();
			timer.UpdateInterval = TimeSpan.FromTicks(333333);
			timer.Update += OnUpdate;
			timer.Draw += OnDraw;

			// Use the LayoutUpdate event to know when the page layout 
			// has completed so that we can create the UIElementRenderer.
			LayoutUpdated += new EventHandler(GamePage_LayoutUpdated);
		}

		void GamePage_LayoutUpdated(object sender, EventArgs e)
		{
			// Create the UIElementRenderer to draw the XAML page to a texture.

			// Check for 0 because when we navigate away the LayoutUpdate event
			// is raised but ActualWidth and ActualHeight will be 0 in that case.
			if ((ActualWidth > 0) && (ActualHeight > 0))
			{
				SharedGraphicsDeviceManager.Current.PreferredBackBufferWidth = (int)ActualWidth;
				SharedGraphicsDeviceManager.Current.PreferredBackBufferHeight = (int)ActualHeight;

				if ((int)InfoBox.ActualHeight != elementRendererHeight || (int)InfoBox.ActualWidth != elementRendererWidth)
				{
					elementRenderer = new UIElementRenderer(InfoBox, (int)InfoBox.ActualWidth, (int)InfoBox.ActualHeight);
					elementRendererHeight = (int)InfoBox.ActualHeight;
					elementRendererWidth = (int)InfoBox.ActualWidth;
				}

				if ((int)RandomImage.ActualHeight != elementRenderer2Height || (int)RandomImage.ActualWidth != elementRenderer2Width)
				{
					elementRenderer2 = new UIElementRenderer(RandomImage, (int)RandomImage.ActualWidth, (int)RandomImage.ActualHeight);
					elementRenderer2Height = (int)RandomImage.ActualHeight;
					elementRenderer2Width = (int)RandomImage.ActualWidth;
				}
			}

			if (null == elementRenderer)
			{
				elementRenderer = new UIElementRenderer(InfoBox, (int)InfoBox.ActualWidth, (int)InfoBox.ActualHeight);
				elementRenderer2 = new UIElementRenderer(RandomImage, (int)RandomImage.ActualWidth, (int)RandomImage.ActualHeight);
			}
		}

		protected override void OnNavigatedTo(NavigationEventArgs e)
		{
			// Set the sharing mode of the graphics device to turn on XNA rendering
			SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);

			// Create a new SpriteBatch, which can be used to draw textures.
			spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);

			// TODO: use this.content to load your game content here
			LoadContent();

			// Start the timer
			timer.Start();

			base.OnNavigatedTo(e);
		}

		/// <summary>
		/// LoadContent will be called once per game and is the place to load
		/// all of your content.
		/// </summary>
		void LoadContent()
		{
		}

		protected override void OnNavigatedFrom(NavigationEventArgs e)
		{
			// Stop the timer
			timer.Stop();

			// Set the sharing mode of the graphics device to turn off XNA rendering
			SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);

			base.OnNavigatedFrom(e);
		}

		/// <summary>
		/// Allows the page to run logic such as updating the world,
		/// checking for collisions, gathering input, and playing audio.
		/// </summary>
		private void OnUpdate(object sender, GameTimerEventArgs e)
		{
		}

		/// <summary>
		/// Allows the page to draw itself.
		/// </summary>
		private void OnDraw(object sender, GameTimerEventArgs e)
		{
			// Render the Silverlight controls using the UIElementRenderer.
			elementRenderer.Render();
			elementRenderer2.Render();

			SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.CornflowerBlue);

			// TODO: Add your drawing code here
			spriteBatch.Begin();

			// Using the texture from the UIElementRenderer, 
			// draw the Silverlight controls to the screen.
			spriteBatch.Draw(elementRenderer.Texture, Vector2.Zero, Color.White);
			spriteBatch.Draw(elementRenderer2.Texture, Vector2.Zero, Color.White);

			spriteBatch.End();
		}
	}
}